﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Controller;
using Temp.Model;
using Temp.NLG;
using Temp.View;

namespace Temp.Planner2
{
    class EventPlanner
    {
        private string nameOfTriggerer;
        private string nameOfChosenOption;
        private string typeOfTrigerer;
        private ItemController ic;
        private StoryNodeController sc;
        public List<Item> itemList { get; set; }
        private List<Item> listInventory { get; set; }
        private StoryPlanner sp;
        private AlexGame alexGame;
        private ComicStripDrawer comicDrawer;
        public DialogueView dv { get; set; }
        public PeerPlanner peerPlanner { get; set; }
        private Describer describer;
        public Boolean loadAgain = false;
        private List<string> activityLog;
        /// <summary>
        /// Constructor for the EventPlanner class
        /// </summary>
        /// <param name="itemList">list of items in the world</param>
        /// <param name="listInventory">list of items in the inventory</param>
        /// <param name="sp">Instance of the StoryPlanner class</param>
        /// <param name="alexGame">Instance of the AlexGame class</param>
        /// <param name="comicDrawer">Instace of the ComicStripDrawe class</param>
        /// <param name="activityLog">list of activities done by the user</param>
        public EventPlanner(List<Item> itemList, List<Item> listInventory, StoryPlanner sp, AlexGame alexGame, ComicStripDrawer comicDrawer, List<string> activityLog)
        {
            ic = new ItemController();
            sc = new StoryNodeController();

            this.itemList = itemList;
            this.listInventory = listInventory;
            this.sp = sp;
            this.dv = dv;
            this.alexGame = alexGame;
            this.comicDrawer = comicDrawer;
            describer = new Describer(sp);
            this.activityLog = activityLog;
        }

        /// <summary>
        /// Method to be called once an event has been triggered
        /// </summary>
        /// <param name="nameOfTriggerer">Name of item/person that triggered the event</param>
        /// <param name="nameOfChosenOption">Option chosen by the user</param>
        /// <param name="typeOfTrigerer">Type of the trigger</param>
        public void Initialize(string nameOfTriggerer, string nameOfChosenOption, string typeOfTrigerer)
        {
            this.nameOfTriggerer = nameOfTriggerer;
            this.nameOfChosenOption = nameOfChosenOption;
            this.typeOfTrigerer = typeOfTrigerer;
            triggerEvent();
        }

        /// <summary>
        /// Triggers the event 
        /// </summary>
        private void triggerEvent()
        {
            Ontology sn = sc.getEvent(nameOfChosenOption);
            System.Diagnostics.Debug.WriteLine("option:" + nameOfChosenOption + "trigerrer:" + nameOfTriggerer);
            //System.Diagnostics.Debug.WriteLine("story node trigger:" + sn.concept2);
            if (sn != null)
            {
                System.Diagnostics.Debug.WriteLine("node: " + sn.concept1 +" trigger: "+sn.concept2.ToString());
                if (sn.concept2.ToString() == "STORE_INVENTORY")
                {
                    addToInventory();
                    loadAgain = true;
                    activityLog.Add("keep " + nameOfTriggerer);
                    peerPlanner.calculateAcitvityListValue("keep " + nameOfTriggerer);
                    
                }

                else if (sn.concept2.ToString() == "DRAW_ART")
                {   
                    System.Diagnostics.Debug.WriteLine("comicstrip loaction:" + comicDrawer.comicStripLocation);
                    comicDrawer.comicStripLocation = sc.getPicURLOrGame(nameOfChosenOption);
                    alexGame.drawAlexWorld = false;
                }

                else if (sn.concept2.ToString() == "LOAD_GAME")
                {
                    
                    string gameName = sc.getPicURLOrGame(nameOfChosenOption);
                    System.Diagnostics.Debug.WriteLine("LOAD GMAE!!! " +  gameName);
                    alexGame.drawAlexWorld = false;
                    if (gameName == "PACK_GAME")
                    {
                        alexGame.inMinigame1 = true;
                        System.Diagnostics.Debug.WriteLine("IN PACK GAME");
                    }
                    else if (gameName == "SIMON_SAYS")
                        alexGame.inMinigame2 = true;
                    else if (gameName == "SEGREGATE_TRASH")
                        alexGame.inMinigame3 = true;

                    activityLog.Add(gameName);
                    peerPlanner.calculateAcitvityListValue(gameName);
                }

                else if (sn.concept2.ToString() == "SHOW_DIALOGUE")
                {
                    string nameOfSpeaker = sc.getSpeaker(nameOfChosenOption);
                    System.Diagnostics.Debug.WriteLine("SPEAKER NAME:" + nameOfSpeaker);
                    dv.listDialogue = describer.getItemDescription(nameOfSpeaker, describer.NPCTYPE);
                    dv.modifyListDialogue();
                    dv.char2Pic = null;
                    dv.isShowing = true;
                }

                else if (sn.concept2.ToString() == "ADD_INVENTORY")
                {
                    List<string> itemToAdd = sc.getItems(nameOfChosenOption);
                    foreach (string s in itemToAdd)
                    {
                        if (listInventory.Count <= sp.MAX_CAPACITY_INVENTORY)
                        {
                            Item i = ic.getItem(s);
                            itemList.Remove(i);
                            activityLog.Add("keep " + i.name);
                            listInventory.Add(i);
                            peerPlanner.calculateAcitvityListValue("keep " + i.name);
                            System.Diagnostics.Debug.WriteLine("keep:" + i.name);
                        }
                        else { 
                            
                        }
                    }
                }

                else if (sn.concept2.ToString() == "REMOVE_INVENTORY")
                {
                    System.Diagnostics.Debug.WriteLine("REMOVE INVENTORY");
                    removeFromInventory();

                }
                //add to activity log
                sp.reEvaluateStory();
                peerPlanner.peerGuideCritic();
            }


        }

        /// <summary>
        /// Used by trigger event. This is for adding items in the inventory
        /// </summary>
        /// 


        private void removeFromInventory() {
            int temp = listInventory.Count;
            
            for(int i =0; i < temp; i++){
                if(listInventory[i].name == nameOfTriggerer){
                    Item it = listInventory[i];
                    listInventory.Remove(it);
                   
                    Item it2 = ic.getItem(it.name);
                    it2.idPlace = ic.getPlaceId(it.name);
                    itemList.Add(it2);
                }
            }
            
            //remove from activity log the keep 
            temp = activityLog.Count;

            for (int a = 0; a < temp; a++) {
                if (activityLog[a] == "keep " + nameOfTriggerer) {
                    activityLog.Remove("keep " + nameOfTriggerer);
                }
            }

            //calculate activity
            peerPlanner.reduceActivityListValue("keep " + nameOfTriggerer);

            //add back to the world
            
        }

        private void addToInventory()
        {
            Item i = ic.getItem(nameOfTriggerer);
            i.idPlace = ic.getPlaceId(i.name);
            System.Diagnostics.Debug.WriteLine("item x:" + i.x + " item y:" + i.y);
            listInventory.Add(i);

            for (int a = 0; a < itemList.Count; a++)
            {
                if (itemList[a].name == nameOfTriggerer)
                {

                    itemList.RemoveAt(a);
                    a = itemList.Count + 1;
                }
            }

        }
    }


}
